package cn.wolfcode.tieba.advice;

import cn.wolfcode.tieba.exception.LogicException;
import cn.wolfcode.tieba.util.JsonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @author 王金龙
 * @date 2021/2/9 8:39
 */
@ControllerAdvice
public class CommonExceptionHandler {
    @ExceptionHandler(LogicException.class)
    @ResponseBody
    public Object logicExp(HttpServletResponse response, LogicException e) {
        e.printStackTrace();
        response.setContentType("application/json;charset=utf-8");
        return JsonResult.error(JsonResult.CODE_ERROR_PARAM, e.getMessage(), null);
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runtimeExp(HttpServletResponse response, RuntimeException e) {
        e.printStackTrace();
        response.setContentType("application/json;charset=utf-8");
        return JsonResult.defaultError();
    }
}
